package com.example.test2;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

class ItemListviewAdapter  extends BaseAdapter {

    private List<details> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemListviewAdapter(Context context,List<details> objects) {
        Log.d("ccccc", "ItemListviewAdapter: "+objects);
        this.objects=objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_listview, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder)convertView.getTag();
        }
        details details=objects.get(position);
        //判断数据库中的sex字段的数据是否为空，为空时不显示，
        if (TextUtils.isEmpty(details.getSex())){
            holder.itemSex.setVisibility(View.GONE);
        }else {
            holder.itemSex.setVisibility(View.VISIBLE);
            holder.itemSex.setText(details.getSex());
        }
        holder.itemAccount.setText(details.getAccount());
        holder.itemPassword.setText(details.getPassword());
        return convertView;
    }


    protected class ViewHolder {
        private TextView itemAccount;
        private TextView itemPassword;
        private TextView itemSex;

        public ViewHolder(View view) {
            itemAccount = (TextView) view.findViewById(R.id.item_account);
            itemPassword = (TextView) view.findViewById(R.id.item_password);
            itemSex = (TextView) view.findViewById(R.id.item_sex);
        }
    }
}